package com.elot.controller.lot;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.elot.controller.core.BaseController;
import com.elot.core.enums.DayLotEnum;
import com.elot.core.enums.NumberLotEnum;
import com.elot.core.types.DynamicArrayList;
import com.elot.core.utils.Converter;
import com.elot.dto.BankAccountDto;
import com.elot.dto.LotDto;
import com.elot.service.LotService;
import com.elot.service.UserService;

@Controller
@RequestMapping("/lot")
public class LotController extends BaseController {

	@Autowired
	UserService userService;
	@Autowired
	LotService lotService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String intro(@RequestParam(value = "user", required = false) Long userId, 
							Model model) {
		Collection<LotDto> list = lotService.findAll();
		model.addAttribute("list", list);
		LotDto lot = new LotDto();
		lot.setBankAccounts(new DynamicArrayList<BankAccountDto>(BankAccountDto.class));
		model.addAttribute("lot", lot);
		
//		if (userId!=null) {
//			UserDto user = userService.find(userId);
//			model.addAttribute("bankAccounts", user.getFavoriteBankAccount());
//		}
		
 		model.addAttribute("dayLot", Converter.getKeyValuesByEnum(DayLotEnum.values()));
 		model.addAttribute("numberLot", Converter.getKeyValuesByEnum(NumberLotEnum.values()));
 		
		return "lot/form";
	}

	@RequestMapping(value="/save", method=RequestMethod.POST)
    public String save(LotDto lot, Model model) {
		lot.setSeo(lot.getTitle().trim().replace(" ", "-"));
		lotService.save(lot);
        return intro(null, model);
    }
	
	@RequestMapping(value="/view/{urlSeo}/{id}", method=RequestMethod.GET)
	public String view(@PathVariable(value = "urlSeo") String urlSeo, 
						@PathVariable(value = "id") Long id, 
						Model model) {
		LotDto lot = lotService.find(id);
		model.addAttribute("lot", lot);
		return "lot/view";
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model) {
		Collection<LotDto> lots = lotService.findAll();
		model.addAttribute("lots", lots);
		return "lot/list";
	}

}
